//
//  WDJHomeController.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJHomeController.h"
#import "WDJWorldDayController.h"
#import "WDJMainController.h"
#import "WDJLunPanController.h"
#import "WDJVipController.h"
#import "WDJAuthManger.h"
@interface WDJHomeController ()
@property (nonatomic,strong) UIButton *wdjGuideBtn;
@property (nonatomic,strong) NSArray *wdjGuides;
@property (nonatomic,assign) NSInteger wdjGuideIndex;

@property (nonatomic,strong) UILabel *worldDayZhLab;
@property (nonatomic,strong) UILabel *worldDayEnLab;
@end

@implementation WDJHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = true;
    self.wdjGuides = @[@"wdj_1",@"wdj_2",@"wdj_3"];
    self.wdjGuideIndex = 0;
    [self wdjCreatView];
    [self worldDayData];
}
- (void)wdjCreatView{
    UIImageView * worldHomeImgView = [[UIImageView alloc] init];
    worldHomeImgView.image = [WDjWholeData wholeImageNamed:@"wdj_homeBg"];
    worldHomeImgView.userInteractionEnabled = true;
    [self.view addSubview:worldHomeImgView];
    [worldHomeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    UIButton * worldSetBtn = [[UIButton alloc] init];
    [worldSetBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeSetBg"] forState:UIControlStateNormal];
    [worldSetBtn addTarget:self action:@selector(worldSet) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:worldSetBtn];
    [worldSetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:68]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:68]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:24]+[WDjWholeData wholeTopSafeH]));
    }];
    UIButton * worldDayBtn = [[UIButton alloc] init];
    [worldDayBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeTopBg"] forState:UIControlStateNormal];
    [worldDayBtn addTarget:self action:@selector(worldDay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:worldDayBtn];
    [worldDayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:30]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:30]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:251]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:116]+[WDjWholeData wholeTopSafeH]));
    }];
    self.worldDayZhLab = [[UILabel alloc] init];
    self.worldDayZhLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:32] weight:UIFontWeightMedium];
    self.worldDayZhLab.textColor = [WDjWholeData wholeColorHexString:@"#FFFFFF"];
    self.worldDayZhLab.numberOfLines = 2;
    [worldDayBtn addSubview:self.worldDayZhLab];
    [self.worldDayZhLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:51]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:173]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:76]));
    }];
    self.worldDayEnLab = [[UILabel alloc] init];
    self.worldDayEnLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:24] weight:UIFontWeightRegular];
    self.worldDayEnLab.textColor = [WDjWholeData wholeColorHexString:@"#4D5B76"];
    self.worldDayEnLab.numberOfLines = 1;
    [worldDayBtn addSubview:self.worldDayEnLab];
    [self.worldDayEnLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:51]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:253]));
        make.top.equalTo(self.worldDayZhLab.mas_bottom).offset([WDjWholeData wholeScreewRatio:14]);
    }];
    UIButton * shiDuBtn = [[UIButton alloc] init];
    [shiDuBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeShiDuBg"] forState:UIControlStateNormal];
    [shiDuBtn addTarget:self action:@selector(shiDu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shiDuBtn];
    [shiDuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:30]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:410]));
        make.top.equalTo(worldDayBtn.mas_bottom).offset([WDjWholeData wholeScreewRatio:54.5]);
    }];
//    UIButton * weatherForecastBtn = [[UIButton alloc] init];
//    [weatherForecastBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeReportBg"] forState:UIControlStateNormal];
//    [weatherForecastBtn addTarget:self action:@selector(weatherForecast) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:weatherForecastBtn];
//    [weatherForecastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@([WDjWholeData wholeScreewRatio:30]));
//        make.height.equalTo(@([WDjWholeData wholeScreewRatio:410]));
//        make.top.equalTo(worldDayBtn.mas_bottom).offset([WDjWholeData wholeScreewRatio:54.5]);
//    }];
    UIButton * wenDuBtn = [[UIButton alloc] init];
    [wenDuBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeWenDuBg"] forState:UIControlStateNormal];
    [wenDuBtn addTarget:self action:@selector(wenDu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wenDuBtn];
    [wenDuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(shiDuBtn.mas_right).offset([WDjWholeData wholeScreewRatio:36]);
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:30]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:410]));
        make.width.equalTo(shiDuBtn);
        make.top.equalTo(shiDuBtn);
    }];
   
    UIButton * lunPanBtn = [[UIButton alloc] init];
    [lunPanBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_homeLunPanBg"] forState:UIControlStateNormal];
    [lunPanBtn addTarget:self action:@selector(lunPan) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lunPanBtn];
    [lunPanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:30]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:410]));
        make.top.equalTo(shiDuBtn.mas_bottom).offset([WDjWholeData wholeScreewRatio:30]);
//        make.left.equalTo(shiDuBtn.mas_right).offset([WDjWholeData wholeScreewRatio:36]);
//        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:30]));
//        make.height.equalTo(@([WDjWholeData wholeScreewRatio:410]));
//        make.width.equalTo(shiDuBtn);
//        make.top.equalTo(shiDuBtn);
    }];
    self.wdjGuideBtn = [[UIButton alloc] init];
    self.wdjGuideBtn.hidden = true;
    self.wdjGuideBtn.userInteractionEnabled = true;
    [self.wdjGuideBtn addTarget:self action:@selector(changeGuide) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.wdjGuideBtn];
    [self.wdjGuideBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@([WDjWholeData wholeScreewRatio:0]));
    }];
    NSString *lunPanGuide = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"lunPanGuide"]];
    if((lunPanGuide.length > 0)&&[lunPanGuide isEqualToString:@"2"]){
        self.wdjGuideBtn.hidden = true;
        [self performSelector:@selector(wdjCheckVipStatuAuth) withObject:self afterDelay:1.0];
        
    }else{
        [self.wdjGuideBtn setBackgroundImage:[WDjWholeData wholeImageNamed:self.wdjGuides[self.wdjGuideIndex]] forState:UIControlStateNormal];
        self.wdjGuideBtn.hidden = false;

    }
}
- (void)wdjCheckVipStatuAuth{
    [self wdjCheckLoginAuth:^{}];
}
- (void)changeGuide{
    self.wdjGuideIndex ++;
    if (self.wdjGuideIndex < 3) {
        [self.wdjGuideBtn setBackgroundImage:[WDjWholeData wholeImageNamed:self.wdjGuides[self.wdjGuideIndex]] forState:UIControlStateNormal];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"lunPanGuide"];
        self.wdjGuideBtn.hidden = true;
        [self wdjCheckLoginAuth:^{
            
        }];
    }
}
- (void)worldDayData{
    NSDictionary * worldDayTitleDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"worldDayTitle"];
    self.worldDayEnLab.text = [worldDayTitleDic objectForKey:@"zhTitle"];
    self.worldDayZhLab.text = [worldDayTitleDic objectForKey:@"enTitle"];
}
- (void)worldSet{
    WDJMainController *mainCtrl = [[WDJMainController alloc] init];
    [self.navigationController pushViewController:mainCtrl animated:true];
}
- (void)worldDay{
    WeakSelf
    [self wdjCheckLoginAuth:^{
        WDJWorldDayController *worldDayCtrl = [[WDJWorldDayController alloc] init];
        [self.navigationController pushViewController:worldDayCtrl animated:true];
    }];
}
- (void)weatherForecast{
    WeakSelf
    [self wdjMakeAuth:^{
        WDJWebController * wdjWebCtrl = [[WDJWebController alloc] init];
        wdjWebCtrl.wdjWebUrl = [WDjWholeData wdjUrlForCityName];
        wdjWebCtrl.isWhiteBack = true;
        [weakSelf.navigationController pushViewController:wdjWebCtrl animated:true];
    }];
}
- (void)wenDu{
    WeakSelf
    [self wdjMakeAuth:^{
        WDJWebController * wdjWebCtrl = [[WDJWebController alloc] init];
        wdjWebCtrl.wdjWebUrl = [WDjWholeData wdjUrlForWenDu];
        wdjWebCtrl.isWhiteBack = true;
        [weakSelf.navigationController pushViewController:wdjWebCtrl animated:true];
    }];
}
- (void)shiDu{
    WeakSelf
    [self wdjMakeAuth:^{
        WDJWebController * wdjWebCtrl = [[WDJWebController alloc] init];
        wdjWebCtrl.wdjWebUrl = [WDjWholeData wdjUrlForShiDu];
        wdjWebCtrl.isWhiteBack = true;
        [weakSelf.navigationController pushViewController:wdjWebCtrl animated:true];
    }];
}
- (void)lunPan{
    WeakSelf
    [self wdjCheckLoginAuth:^{
        WDJLunPanController *lunPanCtrl = [[WDJLunPanController alloc] init];
        [weakSelf.navigationController pushViewController:lunPanCtrl animated:true];
    }];
}
- (void)wdjMakeAuth:(void(^)(void))sucBlock{
    NSTimeInterval wdjVipBuyTime = (NSTimeInterval)[[NSUserDefaults standardUserDefaults] doubleForKey:@"wdjVipBuyTime"];
    NSTimeInterval wdjCurrentTime = [[NSDate date] timeIntervalSince1970];
    if(!wdjVipBuyTime||(wdjVipBuyTime <= wdjCurrentTime)){
        WDJVipController * wdjVipCtrl = [[WDJVipController alloc] init];
        UINavigationController * wdjVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:wdjVipCtrl];
        wdjVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:wdjVipNavCtrl animated:true completion:^{}];
    }else{
        if ([WDJPublicData defaultManager].wdjLocalLat&&[WDJPublicData defaultManager].wdjLocalLon&&[WDJPublicData defaultManager].wdjLocalCityName) {
            sucBlock();
        }else{
            [WDJAuthManger requestLocationAuth:^{
            }];
        }
    }
}
- (void)wdjCheckLoginAuth:(void(^)(void))sucBlock{
    NSTimeInterval wdjVipBuyTime = (NSTimeInterval)[[NSUserDefaults standardUserDefaults] doubleForKey:@"wdjVipBuyTime"];
    NSTimeInterval wdjCurrentTime = [[NSDate date] timeIntervalSince1970];
    if(!wdjVipBuyTime||(wdjVipBuyTime <= wdjCurrentTime)){
        WDJVipController * wdjVipCtrl = [[WDJVipController alloc] init];
        UINavigationController * wdjVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:wdjVipCtrl];
        wdjVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:wdjVipNavCtrl animated:true completion:^{}];
    }else{
        sucBlock();
    }
}
- (void)wdjCheckVipAuth:(void(^)(void))sucBlock{
    if ([WDJPublicData defaultManager].wdjLocalLat&&[WDJPublicData defaultManager].wdjLocalLon&&[WDJPublicData defaultManager].wdjLocalCityName) {
        sucBlock();
    }else{
        [WDJAuthManger requestLocationAuth:^{
        }];
    }
}
@end
